package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.Impl.BrandServiceImpl;
import com.itheima.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends HttpServlet {
    private BrandService sr = new BrandServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String ru = request.getRequestURI();
        String[] split = ru.split("/");
        String s = split[split.length-1];
        if ("selall".equals(s)){
            this.selectall(request,response);
        }else if("add".equals(s)){
            this.add(request,response);
        }else if("delid".equals(s)){
            this.delid(request,response);
        }else if("seland".equals(s)){
            this.seland(request,response);
        }else if("updbyid".equals(s)){
            this.updbyid(request,response);
        }
    }

    private void updbyid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        sr.updBrand(brand);
        response.getWriter().write("true");
    }

    private void seland(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer dqys = Integer.valueOf(request.getParameter("dqys"));
        Integer myts = Integer.valueOf(request.getParameter("myts"));

        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);

        Page page = sr.getallAnd(dqys,myts,brand);

        String s1 = JSON.toJSONString(page);

        response.getWriter().write(s1);


    }

    private void delid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Integer[] ints = JSON.parseObject(s, Integer[].class);
        sr.deleteByIds(ints);
        response.getWriter().write("true");
    }


    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand parse = JSON.parseObject(s,Brand.class);
        sr.ins(parse);
        response.getWriter().write("true");
    }

    private void selectall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer dqys = Integer.valueOf(request.getParameter("dqys"));
        Integer myts = Integer.valueOf(request.getParameter("myts"));

        Page getall = sr.getall(dqys, myts);
        PrintWriter writer = response.getWriter();
        String s = JSON.toJSONString(getall);
        writer.write(s);
    }

    public void ff(){
        System.out.println("H和H和");
    }

    public void ss(){
        System.out.println("SSSS");
    }


}
